
package com.kingdee.eas.custom.aoyuan.aytime.bizbill.web;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import javax.faces.FacesException;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;

import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.component.action.ActionEvent;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.dao.AbstractBaseObjectCollection;
import com.kingdee.bos.dao.query.IQueryExecutor;
import com.kingdee.bos.dao.query.ISQLExecutor;
import com.kingdee.bos.dao.query.QueryExecutorFactory;
import com.kingdee.bos.dao.query.SQLExecutorFactory;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.bos.metadata.MetaDataPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.sql.ParserException;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.bos.waf.annotation.IBOSBizCtrl;
import com.kingdee.bos.waf.ctx.WafContext;
import com.kingdee.bos.waf.exception.WafBOSException;
import com.kingdee.bos.waf.winlet.list.ListBean;
import com.kingdee.bos.web.components.widget.impl.UIPromptBox;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.framework.ICoreBase;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.FullOrgUnitInfo;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.basedata.org.PositionInfo;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.custom.aoyuan.aytime.ManagerWebUtil;
import com.kingdee.eas.custom.aoyuan.aytime.bizbill.IPersonOTBalance;
import com.kingdee.jdbc.rowset.IRowSet;

@ManagedBean(name = "custom.aoyuan.aytime.bizbill.PersonOTBalanceListUIBean", scope = ManagedBeanScope.SESSION)
public class PersonOTBalanceListUIBean extends AbstractPersonOTBalanceListUIBean {

	   public PersonOTBalanceListUIBean()
	    {
	    	displayRows = 20;
	    	
	    }
	        
	    @Bind
	    private String showHeight;
	    
	    @Bind
	    private int displayRows;
	    
	    @Bind
	    private boolean isManager=false;
	    
	    public void pageOnload()
	    {	
	    	setEntityViewInfoOnload();
	    	super.pageOnload();
	    	initShowHeight(); 
	    	getBalaceList(null);
	    }
	    
	    public void onRender(boolean isPostBack)
        {
        	super.onRender(isPostBack);
        	Map params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        	String filterInfo = (String)params.get("filterInfo");
            if(filterInfo != null && !filterInfo.equals("WHERE "))
            {
                try
                {
					getBalaceList(filterInfo);
				}
					catch(Exception e)
				{
					logger.error("query error:", e);
					throw new FacesException(new WafBOSException(e));
				}            	
            }
        }
	    
	    public void getBalaceList(String filterInfo)
	    {
			PersonInfo personInfo = ContextUtil.getCurrentUserInfo(
					WafContext.getInstance().getContext()).getPerson();
			HROrgUnitInfo hrOrgInfo= ContextUtil.getCurrentHRUnit(WafContext.getInstance().getContext());

			if(hrOrgInfo!=null)
			{
				isManager = ManagerWebUtil.getManagerPermission(hrOrgInfo,"manager002");					
			}
			String sql ="SELECT * FROM V_AY_TIME_OT_STAT t where 1=1 ";
		    if(isManager)
		    {
				if(hrOrgInfo!=null)
				{
					if(filterInfo!=null)
					{
						String filterStr=filterInfo.replace("WHERE person.name", " and personName");
				    	sql+=filterStr;	
				    	sql+=" and t.hrOrgUnitId='"+hrOrgInfo.getId().toString()+"'";		
					}else
					{
				    	sql+=" and t.hrOrgUnitId='"+hrOrgInfo.getId().toString()+"'";									
					}			
				}else
				{
					//没有岗位只查自己
			    	sql+=" and t.personId='"+personInfo.getId().toString()+"'";							
				}
		    }else
		    {
		    	sql+=" and t.personId='"+personInfo.getId().toString()+"'";		    	
		    }
	    	this.listModel = new LinkedList<Map<String,Object>>();
			ISQLExecutor exe=SQLExecutorFactory.getRemoteInstance(sql);
			IRowSet rs;
			try {
				rs = exe.executeSQL();
				while(rs.next()){
			    	Map<String, Object>  listData =new HashMap<String, Object>();
					listData.put("personNo", rs.getString("personNo"));
					listData.put("personName", rs.getString("personName"));
					listData.put("ex1", rs.getString("ex1"));
					listData.put("ex2", rs.getString("ex2"));
					listData.put("ex3", rs.getString("ex3"));					
					listData.put("ex4", rs.getString("ex4"));
					listData.put("ex5", rs.getString("ex5"));
					listData.put("ex6", rs.getString("ex6"));
					listData.put("ex7", rs.getString("ex7"));
					listData.put("ex8", rs.getString("ex8"));
					listData.put("ex9", rs.getString("ex9"));
					listData.put("ex10", rs.getString("ex10"));
					listData.put("ex11", rs.getString("ex11"));
					listData.put("ex12", rs.getString("ex12"));
					listData.put("ex13", rs.getString("ex13"));
					listData.put("ex14", rs.getString("ex14"));
					listData.put("ex15", rs.getString("ex15"));
					listData.put("ex16", rs.getString("ex16"));
					listData.put("ex17", rs.getString("ex17"));
					listData.put("ex18", rs.getString("ex18"));
					listData.put("ex19", rs.getString("ex19"));
					listData.put("ex20", rs.getString("ex20"));
					listData.put("EOF", rs.getString("EOF"));
			    	listModel.add(listData);
				}
		    	grid.reload();
			} catch (BOSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	    	
	    }
	    
	    public void setEntityViewInfoOnload()
	    {
			EntityViewInfo entityViewInfo = new EntityViewInfo();
			FilterInfo filterInfo = new FilterInfo();	 
			PersonInfo personInfo = ContextUtil.getCurrentUserInfo(
					WafContext.getInstance().getContext()).getPerson();
			HROrgUnitInfo hrOrgInfo= ContextUtil.getCurrentHRUnit(WafContext.getInstance().getContext());

			if(hrOrgInfo!=null)
			{
				isManager = ManagerWebUtil.getManagerPermission(hrOrgInfo,"manager002");					
			}
		    if(!isManager)
		    {
			  	filterInfo.getFilterItems().add(new FilterItemInfo("person.id",personInfo.getId().toString(),CompareType.EQUALS));		    	
		    }
			entityViewInfo.setFilter(filterInfo);
			
			//重新设置查询条件
			setEntityViewInfo(entityViewInfo);
	    }
	    
	    private PositionInfo getPosition(PersonInfo personInfo)
	    {
			PositionInfo positionInfo = null;
			EntityViewInfo entityViewInfo = new EntityViewInfo();
			FilterInfo filterInfo = new FilterInfo();
			filterInfo.getFilterItems().add(
					new FilterItemInfo("person.id", personInfo.getId(),
							CompareType.EQUALS));
			entityViewInfo.setFilter(filterInfo);
			IQueryExecutor iqec = QueryExecutorFactory.getRemoteInstance(new MetaDataPK(    
	                "com.kingdee.eas.custom.aoyuan.aytime.basedata.app",    
	                "PersonHRQuery"));
			try {
				iqec.setObjectView(entityViewInfo);  
				IRowSet irow = iqec.executeQuery();
				if(irow.size()>0)
				{
					if(irow.next())
					{
						 positionInfo=new PositionInfo();
						 HROrgUnitInfo hrOrg=new HROrgUnitInfo();
						 AdminOrgUnitInfo adminOrg=new AdminOrgUnitInfo();
						 String positionId=irow.getString("Position.id");
						 String positionName=irow.getString("Position.name");
						 positionInfo.setId(BOSUuid.read(positionId));
						 positionInfo.setName(positionName);
						 String hrOrgId=irow.getString("HROrgUnit.id");
						 String hrOrgName=irow.getString("HROrgUnit.name");
						 hrOrg.setId(BOSUuid.read(hrOrgId));
						 hrOrg.setName(hrOrgName);
						 hrOrg.setLongNumber(irow.getString("HROrgUnit.longNumber"));
						 String adminOrgId=irow.getString("AdminOrgUnit.id");
						 String adminOrgName=irow.getString("AdminOrgUnit.name");
						 adminOrg.setId(BOSUuid.read(adminOrgId));
						 adminOrg.setName(adminOrgName);
						 positionInfo.setAdminOrgUnit(adminOrg);
						 positionInfo.setHrOrgUnit(hrOrg);
					}
				}
				return positionInfo;
			} catch (BOSException e) {
				e.printStackTrace();
				return null;
			} catch (SQLException e) {
				e.printStackTrace();
				return null;
			}
	    }  	    
	    
		protected void actionQueryData_actionPerformed(ActionEvent event) throws AbortProcessingException {
			//queryEntityViewInfo();
			//getBalaceList();
			if(this.listModel==null||listModel.size()==0)
			{
	    		addMessage("没有找到员工调休记录!");		
			}
		}
	    
	    protected String commQueryOptions()
	    {
	       StringBuffer opts = new StringBuffer();
	        opts.append("{");
	        opts.append("PARENT_PAGE_NAME").append(":'");
	        opts.append((new StringBuilder()).append(getClass().getName()).append("',").toString());
	        opts.append("QUERY_NAME").append(":'");
	        opts.append((new StringBuilder()).append(getQueryPK().getFullName()).append("'").toString());
	        String userQuery = getUserQueryView();
	        if(userQuery != null)
	        {
	            opts.append(",");
	            opts.append("USER_QUERY_VIEW").append(":'");
	            opts.append((new StringBuilder()).append(userQuery).append("'").toString());
	        }
	        opts.append("}");
	        return opts.toString();
	    }      
	    
	    protected void initShowHeight()
	    {
	        Object obj = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("clientHeight");
	        if(obj != null)
	            showHeight = obj.toString();
	        else
	            showHeight = "600";
	        Map params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
	        String objMethod = (String)params.get("fromMethod");
	        int tempHeight = Integer.parseInt(showHeight);
	        if("commonFunctionPortlet".equals(objMethod))
	        {
	            showHeight = String.valueOf(tempHeight - 160);
	            displayRows = (tempHeight - 160 - 53) / 20;
	            if(tempHeight - 160 - 53 - displayRows * 20 < 19)
	                displayRows--;
	        } else
	        if("commonFunctionPortlet".equals(objMethod))
	        {
	            showHeight = String.valueOf(tempHeight - 160);
	            displayRows = (tempHeight - 160 - 53) / 20;
	            if(tempHeight - 160 - 53 - displayRows * 20 < 19)
	                displayRows--;
	        } else
	        if("fastToolbar".equals(objMethod))
	        {
	            showHeight = String.valueOf(tempHeight - 160);
	            displayRows = (tempHeight - 160 - 53) / 20;
	            if(tempHeight - 160 - 53 - displayRows * 20 < 19)
	                displayRows--;
	        } else
	        {
	            showHeight = String.valueOf(tempHeight - 170);
	            displayRows = (tempHeight - 170 - 53) / 20;
	            if(tempHeight - 170 - 53 - displayRows * 20 < 19)
	                displayRows--;
	        }
	    }   
	
	
}

